/**
 * Sistem Informasi Penjualan
 * @author Kelompok 1
 * Ligar Sekar Wangi
 * Tri Maryani
 * Winda Rahadyan
 * TIF/IV/A
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
import javax.swing.Timer;

public class Menu_Utama extends JFrame implements ActionListener {
	private JDesktopPane desktop1 		        = new JDesktopPane();
	/* Menu Pulldown */
	private JMenuBar 	barMenu1 			    = new JMenuBar();
	private JMenu 		mnuFileMaster 			= new JMenu("File Master"), 
						mnuFileTransaksi		= new JMenu("File Transaksi"), 
						mnuReport			    = new JMenu("Report"), 
						mnuExit 			    = new JMenu("Exit");
						
	private JMenuItem 	mnuFMPelanggan  		= new JMenuItem("Pelanggan", new ImageIcon ("Gambar/cust.JPG")), 
						mnuFMBarang             = new JMenuItem("Barang", new ImageIcon ("Gambar/prod1.JPG")), 
						mnuReturn1              = new JMenuItem("Return", new ImageIcon ("Gambar/CNFNOT.jpg")),
						
						mnuFTFaktur     		= new JMenuItem("Faktur", new ImageIcon ("Gambar/FACE02.JPG")), 
						mnuReturn2              = new JMenuItem("Return", new ImageIcon ("Gambar/CNFNOT.jpg")),
	 					
	 					mnuReportBarang 		= new JMenuItem("Report Barang"), 
						mnuReportPelanggan		= new JMenuItem("Report Pelanggan"),
						mnuReportFaktur         = new JMenuItem("Report Faktur"), 
	 					mnuReturn3              = new JMenuItem("Return", new ImageIcon ("Gambar/CNFNOT.jpg")),
	 				    
	 				    mnuExit1                = new JMenuItem("Exit", new ImageIcon ("Gambar/CNFNOT.jpg"));
	 				    
	 											

	/* Menu PopUp */
	private JPopupMenu 	popMenu1 			= new JPopupMenu();
	private JMenuItem 	mnuPopUp_FMPelanggan= new JMenuItem("File Master Pelanggan", new ImageIcon ("Gambar/cust.JPG")), 
						mnuPopUp_FMBarang	= new JMenuItem("File Master Barang", new ImageIcon ("Gambar/prod1.JPG")), 
						mnuPopUp_FMReturn   = new JMenuItem("Return", new ImageIcon ("Gambar/CNFNOT.jpg"));	
							
	/* Menu ToolBar */
	private	JToolBar 	toolBar1 			= new JToolBar();
	private	JPanel 		ToolbarPanel1 		= new JPanel(new FlowLayout(FlowLayout.LEFT)),
						ToolbarPanel2 		= new JPanel(new FlowLayout(FlowLayout.RIGHT));
	private	JButton 	mnuBtnFMPelanggan	= new JButton(new ImageIcon ("pelanggan.JPG")), 
						mnuBtnFMBarang      = new JButton(new ImageIcon ("BARANG.JPG")),
						mnuBtnFMExit        = new JButton(new ImageIcon ("exit.jpg"));	
	private JLabel 		lblwaktu1			= new JLabel();

	/* Pengaturan Waktu dan Tanggal */
	private Date tglsekarang = new Date();
	private SimpleDateFormat smpdtfmt = new SimpleDateFormat("dd MMM yyyy", Locale.getDefault());
	private String tanggal1 = smpdtfmt.format(tglsekarang) ;

	/* Pengaturan Status Bar */
	private JPanel 		statusBar1 		= new JPanel();
	private JLabel 		lblExit1		= new JLabel(" " + "Copyright(c) Kelompok 1", JLabel.LEFT), 
						lblTgl1			= new JLabel(" " + tanggal1 + " ", JLabel.RIGHT);

	/* Constructor Menu_Utama */
	public Menu_Utama () 
		{
		
		/* Judul (Title) dan Icon Frame */
		super ("Sistem Informasi Penjualan Created By Kelompok 1");
		setIconImage (getToolkit().getImage("Gambar/SisInven.jpg"));
		
		/* Pengaturan Ukuran dan Lokasi frame di Layar */
		setSize (800, 600);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width  - getWidth()) / 2,
			(Toolkit.getDefaultToolkit().getScreenSize().height - getHeight()) / 2);

		/* Memberi Listener Untuk tombol close isinya memangil method Keluar */
		addWindowListener (new WindowAdapter () {
			public void windowClosing (WindowEvent we) {
				Keluar (); 
			}
		});

		/*Mengatur tombol Hot Key (Alt+huruf)*/
		/* Hot Key di Menu Utama */
		mnuFileMaster.setMnemonic ('M');
		mnuFileTransaksi.setMnemonic ('T');
		mnuReport.setMnemonic ('R');
		mnuExit.setMnemonic ('x');
		
		/* Hot Key di Sub Menu File Master */
		mnuFMPelanggan.setMnemonic ('P');
		mnuFMBarang.setMnemonic ('B');			
		mnuReturn1.setMnemonic ('u');
		
		/* Hot Key di Sub Menu File Transaksi */
		mnuFTFaktur.setMnemonic ('F');    		 
		mnuReturn2.setMnemonic ('u');
	 	
		mnuReportBarang.setMnemonic ('B');
		mnuReportPelanggan.setMnemonic ('l');
		mnuReportFaktur.setMnemonic ('F');//Revisi
		mnuReturn3.setMnemonic ('u');	

		/*Shortcut di File Master */ 						
		mnuFMBarang.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK));
		mnuFMPelanggan.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
		mnuReturn1.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK));
		
		/*Shortcut di File Transaksi */
		mnuFTFaktur.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));    		 
	    mnuReturn2.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK));
        
        /*Shortcut di Report */
        mnuReportBarang.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK));
		mnuReportPelanggan.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_L, Event.CTRL_MASK));
		mnuReportFaktur.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));
	    mnuReturn3.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_U, Event.CTRL_MASK));
        
		/*Shortcut di Exit */
		mnuExit1.setAccelerator (KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK));
		
		/* Mengatur ActionListener Pulldown Menu */
		mnuFMBarang.addActionListener (this);
		mnuFMPelanggan.addActionListener (this);
		mnuReturn1.addActionListener (this);
		mnuFTFaktur.addActionListener (this);
		mnuReturn2.addActionListener (this);
		mnuReportBarang.addActionListener (this);
		mnuReportPelanggan.addActionListener (this);
		mnuReportFaktur.addActionListener (this); 
		mnuReturn3.addActionListener (this);
		mnuExit1.addActionListener (this);

		/* Mengatur Sub Menu */
		mnuFileMaster.add (mnuFMBarang);	
		mnuFileMaster.add (mnuFMPelanggan);
		mnuFileMaster.addSeparator ();
		mnuFileMaster.add (mnuReturn1);
		mnuFileTransaksi.add (mnuFTFaktur);	
		mnuFileTransaksi.addSeparator ();	
	    mnuFileTransaksi.add (mnuReturn2);	
	    mnuReport.add (mnuReportBarang);	
		mnuReport.add (mnuReportPelanggan);
		mnuReport.add (mnuReportFaktur);
		mnuReport.addSeparator ();
		mnuReport.add (mnuReturn3);	
		mnuExit.add (mnuExit1);	
			

		/* Mengatur Bar Menu */
		setJMenuBar (barMenu1);

		/* Menambah Baris Ke Menu Bar */
		barMenu1.add (mnuFileMaster); 
		barMenu1.add (mnuFileTransaksi); 
		barMenu1.add (mnuReport); 
		barMenu1.add (mnuExit); 

		/* Mengatur ActionListener PopUp Menu */
		mnuPopUp_FMBarang.addActionListener (this);
		mnuPopUp_FMPelanggan.addActionListener (this);
		mnuPopUp_FMReturn.addActionListener (this);
	
		/* Menambah Submenu PopUp Ke Objek PopUp */
		popMenu1.add (mnuPopUp_FMBarang); 
		popMenu1.add (mnuPopUp_FMPelanggan); 
	    popMenu1.add (mnuPopUp_FMReturn);

		/* Mengatur AddMouseListener pada waktu Clik Kanan PopUp  */
		addMouseListener (new MouseAdapter () {
			public void mousePressed (MouseEvent me) { checkMouseTrigger (me); }
			public void mouseReleased (MouseEvent me) { checkMouseTrigger (me); }
			private void checkMouseTrigger (MouseEvent me) {
				if (me.isPopupTrigger ())
					popMenu1.show (me.getComponent (), me.getX (), me.getY ());
			}
		});
		
		/* Mengatur ToolTip Mouse di atas Objek */
		mnuBtnFMBarang.setToolTipText ("File Master Barang");			
		mnuBtnFMPelanggan.setToolTipText ("File Master Pelanggan");				
		mnuBtnFMExit.setToolTipText ("Exit");		
				

		/* Mengatur addActionListener sehingga Objek saat di klik mengerti */
		mnuBtnFMBarang.addActionListener (this);
		mnuBtnFMPelanggan.addActionListener (this);
		mnuBtnFMExit.addActionListener (this);
		
		/* Meletakkan Objek tombol ke ToolbarPanel1 */
		ToolbarPanel1.add (mnuBtnFMBarang);
		ToolbarPanel1.add (mnuBtnFMPelanggan);
	    ToolbarPanel1.add (mnuBtnFMExit);

		/* Meletakkan Objek tombol ke ToolbarPanel2 */
		lblwaktu1.setForeground(Color.red);
		lblwaktu1.setFont(new Font("Arial",Font.BOLD,30));
		ToolbarPanel2.add (lblwaktu1);
	
		/* Panel ToolbarPanel1, ToolbarPanel2 diletakkan di Toolbar */ 
		toolBar1.add (ToolbarPanel1);
		toolBar1.add (ToolbarPanel2);

		/* Mengatur Baris Status */
		statusBar1.setLayout (new BorderLayout());
		statusBar1.setFont(new Font("Arial",Font.BOLD,10));
		
		/* Meletakkan objek Label ke Baris Status */
		statusBar1.add (lblExit1, BorderLayout.WEST);
		statusBar1.add (lblTgl1, BorderLayout.EAST);

		/* Mengatur Warna Latar Belakang */
		desktop1.setBackground(Color.gray);
		
		/* Menambah Isi Container Dengan Objek toolbar, desktop dan statusbar */
		getContentPane().add (toolBar1, BorderLayout.NORTH);
		getContentPane().add (desktop1, BorderLayout.CENTER);
		getContentPane().add (statusBar1, BorderLayout.SOUTH);
		
		/* Memanggi (menjalankan) Jam */
		setJam();
		
		/* Menampilkan Menu Utama Ke Layar */
		show();
	}

	/* Metode Memeriksa Objek yang mendapat listener "addActionlistener" */
	public void actionPerformed (ActionEvent ae) {
		Object obj = ae.getSource();
		
		if (obj == mnuFMBarang || obj == mnuPopUp_FMBarang || obj == mnuBtnFMBarang) {
			Barang FMBarang = new Barang(); 
			desktop1.add (FMBarang);
			FMBarang.show ();
		}
		
	   	if (obj == mnuFTFaktur) {
			Faktur FMFaktur = new Faktur(); 
			desktop1.add (FMFaktur);
			FMFaktur.show ();
		}

		if (obj == mnuReportBarang) {
			ReportBarang RptBrg = new ReportBarang(); 
			desktop1.add (RptBrg);
			RptBrg.show ();
		}
		
		if (obj == mnuReportPelanggan) {
			ReportPelanggan RptPlg = new ReportPelanggan(); 
			desktop1.add (RptPlg);
			RptPlg.show ();
		}
		
		if (obj == mnuReportFaktur) {
			ReportFaktur RptFkt = new ReportFaktur(); 
			desktop1.add (RptFkt);
			RptFkt.show ();
		} 
		
		if (obj == mnuFMPelanggan || obj == mnuPopUp_FMPelanggan || obj == mnuBtnFMPelanggan) {
			Pelanggan FMPelanggan = new Pelanggan(); 
			desktop1.add (FMPelanggan);
			FMPelanggan.show ();
		}
	
		if (obj==mnuReturn1 || obj==mnuReturn2 || obj==mnuReturn3 || obj==mnuPopUp_FMReturn) {
			new Menu_Utama (); 
		}
		if (obj==mnuExit1 || obj==mnuBtnFMExit) {
			Keluar (); 
		}
	}

	/* Methode Keluar Dari Sistem */
	private void Keluar () {
		try {
	    	int reply = JOptionPane.showConfirmDialog (this,
					"Yakin Mau Keluar?",
					"Sistem Penjualan - Bye", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
			if (reply == JOptionPane.YES_OPTION) {
				setVisible (false);
				dispose();         
				System.out.println ("Thank !!!");
				System.out.println ("Kelompok 1");
				System.out.println ("\n2009");
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
				System.exit (0);        
			}
		} 
		catch (Exception e) {
		}
	}

	/* Methode Setting Waktu Sistem */
	public void setJam() {
		ActionListener taskPerformer = new ActionListener() {
    		public void actionPerformed(ActionEvent evt) {
        		String nol_jam = "", nol_menit = "", nol_detik = "";
    	   
        		Date dateTime = new Date();
        		int nilai_jam = dateTime.getHours();
        		int nilai_menit = dateTime.getMinutes();
	        	int nilai_detik = dateTime.getSeconds();
    	    
        		if (nilai_jam <= 9) nol_jam = "0";
        		if (nilai_menit <= 9) nol_menit = "0";     
        		if (nilai_detik <= 9) nol_detik = "0";
        
	        	String jam = nol_jam + Integer.toString(nilai_jam);
    	    	String menit = nol_menit + Integer.toString(nilai_menit);
        		String detik = nol_detik + Integer.toString(nilai_detik);
        
        		lblwaktu1.setText(jam + ":" + menit + ":" + detik + " ");
      		}
   		};
    	new Timer(1000, taskPerformer).start();
  	} 	
}